package com.servlet;

import com.dao.DormBuildDao;
import com.dao.StudentDao;
import com.model.DormManager;
import com.model.PageBean;
import com.model.Student;
import com.util.DbUtil;
import com.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

@WebServlet(name = "RegisterServlet")
public class RegisterServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
    StudentDao studentDao = new StudentDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        allSave(request, response);
    }

    private void allSave(HttpServletRequest request,
                             HttpServletResponse response)throws ServletException, IOException {
        String studentId = request.getParameter("studentId");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String dormBuildId = request.getParameter("dormBuildId");
        String dormName = request.getParameter("dormName");
        String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
        String sex = new String(request.getParameter("sex").getBytes("ISO-8859-1"), "UTF-8");
        String tel = request.getParameter("tel");
        Student student = new Student(userName, password, Integer.parseInt(dormBuildId), dormName, name, sex, tel);
        if(StringUtil.isNotEmpty(studentId)) {
            student.setStudentId(Integer.parseInt(studentId));
        }
        Connection con = null;
        try {
            con = dbUtil.getCon();
            int saveNum = 0;
            if(studentDao.haveNameByNumber(con, student.getStuNumber())){
                request.setAttribute("student", student);
                request.setAttribute("error", "该学号已存在");
                request.setAttribute("mainPage", "register.jsp");
                request.getRequestDispatcher("register.jsp").forward(request, response);
                try {
                    dbUtil.closeCon(con);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return;
            } else {
                studentDao.studentAdd(con, student);
                request.setAttribute("error", "注册成功！");
                request.setAttribute("mainPage", "register.jsp");
                request.getRequestDispatcher("register.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
